package demo.utils;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.apache.commons.lang3.StringUtils;
import redis.clients.util.IOUtils;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.bmp.BMPImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;

public class ImageUtil {



    /**
     * 判断网络图片是否存在
     * posturl 图片地址链接
     */
    public static boolean canRead(String source) {
        try {
            URL url = new URL(source);
            URLConnection uc = url.openConnection();
            InputStream in = uc.getInputStream();
            if (source.equalsIgnoreCase(uc.getURL().toString()))
                in.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    static BufferedImage image;

    static void createImage(String fileLocation) {
        try {
            FileOutputStream fos = new FileOutputStream(fileLocation);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
            encoder.encode(image);
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void graphicsGeneration(int h1, int h2, int h3, int h4, int h5) {

        final int X = 10;
        int imageWidth = 300;//图片的宽度
        int imageHeight = 300;//图片的高度
        int columnWidth = 30;//柱的宽度
        int columnHeight = 200;//柱的最大高度

        // ChartGraphics chartGraphics = new ChartGraphics();
        image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.white);
//        Color color = new Color(1,1,1);

        graphics.fillRect(0, 0, imageWidth, imageHeight);
        Color color = Color.decode("#FEFF24");
        graphics.setColor(color);
        graphics.drawRect(X + 1 * columnWidth, columnHeight - h1, columnWidth, h1);
        graphics.drawRect(X + 2 * columnWidth, columnHeight - h2, columnWidth, h2);
        graphics.drawRect(X + 3 * columnWidth, columnHeight - h3, columnWidth, h3);
        graphics.drawRect(X + 4 * columnWidth, columnHeight - h4, columnWidth, h4);
        graphics.drawRect(X + 5 * columnWidth, columnHeight - h5, columnWidth, h5);
        createImage("C:\\Users\\GZLX\\Desktop\\color\\aaa.jpg");
    }

    public static void main(String[] args) throws IOException {
        int[] height = {40,50,16,22,85};

        try {
            for (int i = 0; i < 5; i++) {
//                graphicsGeneration(height[0],height[1],height[2],height[3],height[4]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Image image = ImageIO.read(new File("C:\\Users\\GZLX\\Desktop\\color\\aaa.jpg"));
        System.out.println(image.getSource());


    }
}
